package com.bistu.notepad_zjr;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bistu.notepad_zjr.bean.Note;
import com.bistu.notepad_zjr.util.SpfUtil;
import com.bistu.notepad_zjr.util.TimeChange;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import static java.lang.String.format;


/**
 * 添加内容
 */
public class AddActivity extends AppCompatActivity {

    private EditText etTitle,etContent,etEndTime,etAuthor;

    private NoteDbOpenHelper mNoteDbOpenHelper;     //数据库类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        etTitle = findViewById(R.id.et_title);
        etContent = findViewById(R.id.et_content);
        etEndTime = findViewById(R.id.et_endtime);  //截至日期
        etAuthor = findViewById(R.id.et_author);    //作者
        mNoteDbOpenHelper = new NoteDbOpenHelper(this); //数据库类

        //加载作者
        etAuthor.setText(SpfUtil.loadSPF(this,"Author"));
    }

    //保存的监听
    public void add(View view) throws ParseException {
        String author = etAuthor.getText().toString();
        String title = etTitle.getText().toString();
        String content = etContent.getText().toString();
        String endTimeStr = etEndTime.getText().toString(); //字符串类型的日期

        //添加作者
        SpfUtil.saveSPF(this,"Author",etAuthor.getText().toString());

        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this,"标题不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        Note note = new Note();
        note.setAuthor(author);
        note.setTitle(title);
        note.setContent(content);
        note.setCreatedTime(TimeChange.newTimeToStr());
        note.setEndTime(TimeChange.strToTime(endTimeStr));
        Log.i("note：",note.toString());
        //添加信息
        long row = mNoteDbOpenHelper.insertData(note);
        if (row != -1) {
            Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT);
            //结束界面
            this.finish();
        }else {
            Toast.makeText(this,"添加失败",Toast.LENGTH_SHORT);
        }
    }


    //选择时间
    public void selectTime(View view){
        //获取时间
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String timeStr = format("%d-%02d-%d",year,month+1,dayOfMonth);
                try {
                    //选择的时间戳
                    long timeVal = TimeChange.strToTime(timeStr);
                    long newTime = new Date().getTime();
                    if ( TimeChange.strToTime(timeStr) < new Date().getTime()){
                        Toast.makeText(AddActivity.this,"时间选择错！",Toast.LENGTH_SHORT).show();
                    }else {
                        etEndTime.setText(timeStr);
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)
        ).show();
    }

}